package slim3.controller.bot;

import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import slim3.meta.TweetedMeta;
import slim3.model.Tweet;
import slim3.model.Tweeted;
import slim3.service.TwitterService;
import slim3.service.TwitteredService;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;

public class IndexController extends Controller {

    private TwitterService service = new TwitterService();
    private TwitteredService twitteredService = new TwitteredService();

    @Override
    public Navigation run() {

        List<Tweet> tweetList = service.getTweetList();
        List<Tweeted> tweetedList = twitteredService.getTweetedList();

        Twitter twitter4j =
            new TwitterFactory().getInstance("hoge", "hoge");

        for (Tweet tweet : tweetList) {
//            try {

                TweetedMeta e = TweetedMeta.get();
                Tweeted tweeted =
                    Datastore.query(e).filter(e.content.equal(tweet.getContent())).asSingle();
                if(tweeted == null){
    //                twitter4j.updateStatus(tweet.getContent());
                    twitteredService.tweeted(tweet);
                }
//            } catch (TwitterException e) {
//                e.printStackTrace();
//            }
        }
        return forward("index.jsp");
    }
}
